package me.send.produce.data.controller;

import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.send.business.base.Pager;
import me.send.common.poi.ExcelUtil;
import me.send.common.poi.ExcelUtil.ExcelExportData;
import me.send.data.service.pojo.BrokerWallet;
import me.send.data.service.pojo.BrokerWalletLog;
import me.send.data.service.pojo.UserWallet;
import me.send.data.service.pojo.UserWalletLog;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.BrokerWalletLogService;
import me.send.data.service.service.BrokerWalletService;
import me.send.data.service.service.TreeService;
import me.send.data.service.service.UserWalletLogService;
import me.send.data.service.service.UserWalletService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "wallet")
public class WalletController {

	@Autowired
	private UserWalletLogService userWalletLogService;
	@Autowired
	private UserWalletService userWalletService;
	@Autowired
	private BrokerWalletService brokerWalletService;
	@Autowired
	private BrokerWalletLogService brokerWalletLogService;

	@Autowired
	private TreeService treeService;
	
	@RequestMapping(value = "user/log/excel/export")    
	public void exportExcelForUser(HttpServletRequest request, HttpServletResponse response)     
			throws Exception { 
//获取前台额外传递过来的查询条件  
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_time = request.getParameter("start_time"); 
        String end_time = request.getParameter("end_time");  
        String phone = request.getParameter("phone");  
        String changeType = request.getParameter("changeType");  
        String payWay = request.getParameter("payWay");  
        Integer paramPayWay=null;
        Integer paramChangeType=null;
        if(payWay==null||(payWay!=null&&"".equals(payWay.trim()))){
        	payWay=null;
        }else{
        	paramPayWay=Integer.parseInt(payWay);
        }
        if(changeType==null||(changeType!=null&&"".equals(changeType.trim()))){
        	changeType=null;
        }else {
        	paramChangeType=Integer.parseInt(changeType);
        }
        if(phone==null||(phone!=null&&"".equals(phone.trim()))){
        	phone=null;
        }
        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
        	Date startDate=null;
        	Date endDate=null;
        	try {
        		startDate=sdf.parse(start_time);
        		start_time=format.format(startDate);
        		endDate=sdf.parse(end_time);
        		end_time=format.format(endDate);
        	} catch (ParseException e) {
        		e.printStackTrace();
        	}
        }else{
        	start_time=null;
        	end_time=null;
        }
		
		Integer count=userWalletLogService.count(phone,paramChangeType,start_time,end_time,paramPayWay);
		Pager page=new Pager();
		page.setRows(count);
		Integer p=(0/count)+1;
		page.setPage(p);
		
		//随便组织的查询结果  
//		List<BrokerWalletLog> list=brokerWalletLogService.list(phone,start_time,end_time,paramPayWay,page); 
		List<UserWalletLog> list=userWalletLogService.list(phone,paramChangeType,start_time,end_time,paramPayWay,page); 
		List<String[]> columNames = new ArrayList<String[]>();
		columNames.add(new String[] { "雇主手机号", "交易类型（0：充值，1：代办，2：代购，3：代送）", "交易描述", "交易金额", "交易时间", "呼单ID（交易类型为0时不是呼单ID）"});
		
		List<String[]> fieldNames = new ArrayList<String[]>();
		fieldNames.add(new String[] { "telephone", "changeType", "changeDesc", "amount", "changeTime", "orderId" });
		
		LinkedHashMap<String, List<?>> map = new LinkedHashMap<String, List<?>>();
		map.put("雇主钱包流水", list);
		
		
		ExcelExportData setInfo = new ExcelExportData();
		setInfo.setDataMap(map);
		setInfo.setFieldNames(fieldNames);
		setInfo.setTitles(new String[] { "雇主钱包流水报表"});
		setInfo.setColumnNames(columNames);
		
		
		response.setContentType("application/vnd.ms-excel");    
		response.setHeader("Content-disposition", "attachment;filename=user_wallet_log.xls");    
		OutputStream ouputStream = response.getOutputStream();    
		ouputStream.write(ExcelUtil.export2ByteArray(setInfo));
		ouputStream.flush();    
		ouputStream.close();    
	}    
	@RequestMapping(value = "broker/log/excel/export")    
    public void exportExcelForBroker(HttpServletRequest request, HttpServletResponse response)     
    throws Exception { 
//获取前台额外传递过来的查询条件  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_time = request.getParameter("start_time"); 
        String end_time = request.getParameter("end_time");  
        String phone = request.getParameter("phone");  
        String payWay = request.getParameter("payWay");  
        Integer paramPayWay=null;
        if(payWay==null||(payWay!=null&&"".equals(payWay.trim()))){
        	payWay=null;
        }else{
        	paramPayWay=Integer.parseInt(payWay);
        }
        if(phone==null||(phone!=null&&"".equals(phone.trim()))){
        	phone=null;
        }
        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
        	Date startDate=null;
        	Date endDate=null;
        	try {
        		startDate=sdf.parse(start_time);
        		start_time=format.format(startDate);
        		endDate=sdf.parse(end_time);
        		end_time=format.format(endDate);
        	} catch (ParseException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}
        }else{
        	start_time=null;
        	end_time=null;
        }
        
        Integer count=brokerWalletLogService.count(phone,start_time,end_time,paramPayWay);
        Pager page=new Pager();
        page.setRows(count);
        Integer p=(0/count)+1;
        page.setPage(p);
        
                //随便组织的查询结果  
        List<BrokerWalletLog> list=brokerWalletLogService.list(phone,start_time,end_time,paramPayWay,page); 
	 	List<String[]> columNames = new ArrayList<String[]>();
        columNames.add(new String[] { "经纪人手机号", "交易描述", "代购费", "小费", "交易时间", "呼单ID（数据为空表示提现）", "提现订单号"});

        List<String[]> fieldNames = new ArrayList<String[]>();
        fieldNames.add(new String[] { "telephone", "changeDesc", "purchaseAmount", "tipAmount", "changeTime", "orderId", "orderNo" });

        LinkedHashMap<String, List<?>> map = new LinkedHashMap<String, List<?>>();
        map.put("经纪人钱包流水", list);
        
        
        ExcelExportData setInfo = new ExcelExportData();
        setInfo.setDataMap(map);
        setInfo.setFieldNames(fieldNames);
        setInfo.setTitles(new String[] { "经纪人钱包流水报表"});
        setInfo.setColumnNames(columNames);
	 
	 
        response.setContentType("application/vnd.ms-excel");    
        response.setHeader("Content-disposition", "attachment;filename=broker_wallet_log.xls");    
        OutputStream ouputStream = response.getOutputStream();    
        ouputStream.write(ExcelUtil.export2ByteArray(setInfo));
        ouputStream.flush();    
        ouputStream.close();    
   }    

	// 查询提现订单列表
	@RequestMapping(value = "broker/log/list")
	public ModelAndView broker_wallet_log_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/broker/broker_wallet_log");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "wallet/broker/log/list"));
		modelAndView.addObject("module", "经纪人钱包流水列表");
		return modelAndView;
	}
	
	// 查询提现订单列表
	@RequestMapping(value = "broker/list")
	public ModelAndView broker_wallet_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/broker/broker_wallet");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "wallet/broker/list"));
		modelAndView.addObject("module", "经纪人钱包列表");
		return modelAndView;
	}
	
	// 查询提现订单列表
	@RequestMapping(value = "user/log/list")
	public ModelAndView user_wallet_log_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/user_wallet_log");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "wallet/user/log/list"));
		modelAndView.addObject("module", "雇主钱包流水列表");
		return modelAndView;
	}
	
	// 查询提现订单列表
	@RequestMapping(value = "user/list")
	public ModelAndView user_wallet_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/user_wallet");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "wallet/user/list"));
		modelAndView.addObject("module", "雇主钱包列表");
		return modelAndView;
	}

	
	@RequestMapping(value="broker/log/page")  
	@ResponseBody  
	public DatatablesViewPage<BrokerWalletLog> broker_wallet_log_page(HttpServletRequest request){  
//获取分页控件的信息  
		String start = request.getParameter("start");  
		String length = request.getParameter("length"); 
//获取前台额外传递过来的查询条件  
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start_time = request.getParameter("start_time"); 
		String end_time = request.getParameter("end_time");  
		String phone = request.getParameter("phone");  
		String payWay = request.getParameter("payWay");  
		Integer paramPayWay=null;
		if(payWay!=null&&"".equals(payWay.trim())){
			payWay=null;
		}else{
			paramPayWay=Integer.parseInt(payWay);
		}
		if(phone!=null&&"".equals(phone.trim())){
			phone=null;
		}
		if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
			Date startDate=null;
			Date endDate=null;
			try {
				startDate=sdf.parse(start_time);
				start_time=format.format(startDate);
				endDate=sdf.parse(end_time);
				end_time=format.format(endDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else{
			start_time=null;
			end_time=null;
		}
		
		Pager page=new Pager();
		Integer row=Integer.parseInt(length);
		page.setRows(row);
		Integer startInt=Integer.parseInt(start);
		Integer p=(startInt/row)+1;
		page.setPage(p);
		
		//随便组织的查询结果  
		List<BrokerWalletLog> list=brokerWalletLogService.list(phone,start_time,end_time,paramPayWay,page); 
		Integer count=brokerWalletLogService.count(phone,start_time,end_time,paramPayWay);
		DatatablesViewPage<BrokerWalletLog> view = new DatatablesViewPage<BrokerWalletLog>();  
		view.setiTotalDisplayRecords(count);  
		view.setiTotalRecords(count);  
		
		view.setAaData(list);  
		return view;  
	}  
	@RequestMapping(value="broker/page")  
	@ResponseBody  
	public DatatablesViewPage<BrokerWallet> broker_wallet_page(HttpServletRequest request){  
//获取分页控件的信息  
		String start = request.getParameter("start");  
		String length = request.getParameter("length"); 
//获取前台额外传递过来的查询条件  
		String phone = request.getParameter("phone"); 
		if(phone!=null&&"".equals(phone.trim())){
			phone=null;
		}
		String status = request.getParameter("status"); 
		Integer paramStatus=null;
		if(status!=null&&"".equals(status.trim())){
        	status=null;
        }else {
        	paramStatus=Integer.parseInt(status);
        }
		Pager page=new Pager();
		Integer row=Integer.parseInt(length);
		page.setRows(row);
		Integer startInt=Integer.parseInt(start);
		Integer p=(startInt/row)+1;
		page.setPage(p);
		
		//随便组织的查询结果  
		List<BrokerWallet> list=brokerWalletService.list(phone,paramStatus,page); 
		Integer count=brokerWalletService.count(phone,paramStatus);
		DatatablesViewPage<BrokerWallet> view = new DatatablesViewPage<BrokerWallet>();  
		view.setiTotalDisplayRecords(count);  
		view.setiTotalRecords(count);  
		
		view.setAaData(list);  
		return view;  
	}  
	
	@RequestMapping(value="user/log/page")  
    @ResponseBody  
    public DatatablesViewPage<UserWalletLog> user_wallet_log_page(HttpServletRequest request){  
//获取分页控件的信息  
        String start = request.getParameter("start");  
        String length = request.getParameter("length"); 
//获取前台额外传递过来的查询条件  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_time = request.getParameter("start_time"); 
        String end_time = request.getParameter("end_time");  
        String phone = request.getParameter("phone");  
        String changeType = request.getParameter("changeType");  
        String payWay = request.getParameter("payWay");  
        Integer paramPayWay=null;
        Integer paramChangeType=null;
        if(payWay==null||(payWay!=null&&"".equals(payWay.trim()))){
        	payWay=null;
        }else{
        	paramPayWay=Integer.parseInt(payWay);
        }
        if(changeType==null||(changeType!=null&&"".equals(changeType.trim()))){
        	changeType=null;
        }else {
        	paramChangeType=Integer.parseInt(changeType);
        }
        if(phone==null||(phone!=null&&"".equals(phone.trim()))){
        	phone=null;
        }
        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
        	Date startDate=null;
        	Date endDate=null;
        	try {
        		startDate=sdf.parse(start_time);
        		start_time=format.format(startDate);
        		endDate=sdf.parse(end_time);
        		end_time=format.format(endDate);
        	} catch (ParseException e) {
        		e.printStackTrace();
        	}
        }else{
        	start_time=null;
        	end_time=null;
        }
        
        Pager page=new Pager();
        Integer row=Integer.parseInt(length);
        page.setRows(row);
        Integer startInt=Integer.parseInt(start);
        Integer p=(startInt/row)+1;
        page.setPage(p);
        
                //随便组织的查询结果  
        List<UserWalletLog> list=userWalletLogService.list(phone,paramChangeType,start_time,end_time,paramPayWay,page); 
        Integer count=userWalletLogService.count(phone,paramChangeType,start_time,end_time,paramPayWay);
        DatatablesViewPage<UserWalletLog> view = new DatatablesViewPage<UserWalletLog>();  
        view.setiTotalDisplayRecords(count);  
        view.setiTotalRecords(count);  
          
        view.setAaData(list);  
        return view;  
    }  
	@RequestMapping(value="user/page")  
	@ResponseBody  
	public DatatablesViewPage<UserWallet> user_wallet_page(HttpServletRequest request){  
//获取分页控件的信息  
		String start = request.getParameter("start");  
		String length = request.getParameter("length"); 
//获取前台额外传递过来的查询条件  
		String phone = request.getParameter("phone");  
		if(phone!=null&&"".equals(phone.trim())){
			phone=null;
		}
		String status = request.getParameter("status"); 
		Integer paramStatus=null;
		if(status!=null&&"".equals(status.trim())){
        	status=null;
        }else {
        	paramStatus=Integer.parseInt(status);
        }
		Pager page=new Pager();
		Integer row=Integer.parseInt(length);
		page.setRows(row);
		Integer startInt=Integer.parseInt(start);
		Integer p=(startInt/row)+1;
		page.setPage(p);
		
		//随便组织的查询结果  
		List<UserWallet> list=userWalletService.list(phone,paramStatus,page); 
		Integer count=userWalletService.count(phone,paramStatus);
		DatatablesViewPage<UserWallet> view = new DatatablesViewPage<UserWallet>();  
		view.setiTotalDisplayRecords(count);  
		view.setiTotalRecords(count);  
		
		view.setAaData(list);  
		return view;  
	}  
	
}
